﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Eventhere.Work;
using System.IO.IsolatedStorage;

namespace Eventhere
{
    public partial class AddContact : PhoneApplicationPage
    {
        TypesAdd typeAjout;

        public TypesAdd TypeAjout
        {
            get { return typeAjout; }
            set { typeAjout = value; }
        }

        public enum TypesAdd
        {
            Repertory, Event, GroupContact
        }

        public AddContact()
        {
            InitializeComponent();

            this.ExistingContact.Click += Choix1_Click;
            this.CreateContact.Click += Choix2_Click;
            this.ExistingRepertoryContact.Click += Choix3_Click;

            this.ListRepertory.clickcontact += ListRepertory_clickcontact;
            this.ListRepertory.clickgroupcontact += ListRepertory_clickgroupcontact;
            this.OnFragmentNavigation(null);
            

        }

        void ListRepertory_clickgroupcontact(object groupbutton, EventArgs arg2)
        {
            int idGroupContact = (int)((Button)groupbutton).Tag;
            GroupContact g = Work.Repertory.GetGroupById(idGroupContact);
            switch (TypeAjout)
            {
                case TypesAdd.Event:
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentEvent"))
                    {
                        foreach (Contact c in g.ContactGroupContact)
                        {
                            ((Event)IsolatedStorageSettings.ApplicationSettings["CurrentEvent"]).ContactEvent.addContact(c);
                        }
                    }
                    NavigationService.GoBack();

                    break;
                case TypesAdd.GroupContact:
                    break;
                
            }
        }

        void ListRepertory_clickcontact(object contactbutton, EventArgs arg2)
        {
            int idContact = (int)((Button)contactbutton).Tag;
            Contact c = Work.Repertory.GetContactById(idContact);
            switch (TypeAjout)
            {
                case TypesAdd.Event:
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentEvent"))
                    {
                        ((Event)IsolatedStorageSettings.ApplicationSettings["CurrentEvent"]).ContactEvent.addContact(c);

                    }
                    NavigationService.GoBack();

                    break;
                case TypesAdd.GroupContact:
                    break;
                
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string type;
            if (NavigationContext.QueryString.TryGetValue("TypeAjout", out type))
            {
                if (type.Equals(TypesAdd.Event.ToString()))
                    this.TypeAjout = TypesAdd.Event;
                if (typeAjout.Equals(TypesAdd.GroupContact.ToString()))
                    this.TypeAjout = TypesAdd.GroupContact;
                if (type.Equals(TypesAdd.Repertory.ToString()))
                    this.TypeAjout = TypesAdd.Repertory;
            }
            base.OnNavigatedTo(e);
        }


        void Choix3_Click(object sender, RoutedEventArgs e)
        {

            this.PivotP.SelectedItem = this.Existing_Repertory_Contact;

        }

        void Choix2_Click(object sender, RoutedEventArgs e)
        {

            this.PivotP.SelectedItem = this.Creating;
            
        }

        void Choix1_Click(object sender, RoutedEventArgs e)
        {
            this.PivotP.SelectedItem = this.Existing;
            
        }


        private void CreateContact_Click(object sender, RoutedEventArgs e)
        {
            Contact c = new Contact(1, this.TextMailAdress.Text, this.TextFirstName.Text, this.TextLastName.Text);
            switch (TypeAjout)
            {
                case TypesAdd.Event:
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentEvent"))
                    {
                        ((Event)IsolatedStorageSettings.ApplicationSettings["CurrentEvent"]).ContactEvent.addContact(c);
                        
                    }
                    NavigationService.GoBack();
                    
                    break;
                case TypesAdd.GroupContact:
                    break;
                case TypesAdd.Repertory:
                    Repertory rep = new Repertory();
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("Repertoire"))
                    {
                        ((Repertory)(IsolatedStorageSettings.ApplicationSettings["Repertoire"])).addContact(c);
                    }
                    NavigationService.Navigate(new Uri("/Repert/Repert.xaml", UriKind.RelativeOrAbsolute));   
                    break;
             }

            
            
        }
    }
}